import subprocess
import json
from string import Template
import re

class ObjectEncoder(json.JSONEncoder):
  def default(self, obj):
    # Convert objects to a dictionary of their representation
    d = {}
    d.update(obj.__dict__)
    return d

class LatLng(object):
  def __init__(self, lat, long):
    self.lat = lat; self.long = long
  

class Marker(object):
  def __init__(self, attributes):
    for i in attributes:
      if (i == 'position'):
	self.__dict__[i] = attributes[i]
      elif (i == 'title'):
	  res = re.search("\[(?P<unidad>.*)\] (?P<hora>\d+):(?P<minuto>\d+) (?P<kmh>.+)K/h", attributes[i])
	  resdict = res.groupdict()
	  for key in resdict:
	    self.__dict__[key] = resdict[key]

lineaelegida = '517'

lineascompany = ["4_319", "1_500", "2_502", "2_503", "2_504", "2_505", "2_506", "1_507", "2_509", "2_512", "2_513", "1_514", "2_516", "2_517", "2_518", "1_519", "2_5191"]

lineas = {}

for linea in lineascompany:
    lineas[linea[2:]] = linea

linea = lineas[lineaelegida]
    
requestTemplate = Template("""curl -s --data 'r=$linea&t=0' \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'Referer: http://www.gpsurbana.com/' \
http://www.gpsurbana.com/ajax/ebus/get/1234/0""");

json_output = subprocess.check_output(requestTemplate.substitute(linea=linea), shell=True)

json_output = json_output[3:].replace('new google.maps.', '')

json_output = json_output.replace('true', 'True')

for i in ['flat', 'position', 'icon', 'title']:
  json_output = json_output.replace(i + ':', "'%s':" % i)

json_object = eval(json_output)

print ObjectEncoder().encode(json_object[1])
    